package com.example.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Date 2021/7/19 15:33
 * @Created by wangle
 * @Description
 */
@Configuration
public class TtlQueueconfig {

    //普通交换机名称
    public  static  final String exchangeName="X";
    //死信交换机名称
    public  static  final String exchangeName2="Y";
    //普通队列名称
    public  static  final String  queuename1="QA";
    public  static  final String  queuename2="QB";
    public  static  final String  queuename4="QC";
    //sixin队列名称
    public  static  final String  queuename3="QD";


    //声明交换机
    @Bean("xExchange")  //声明xexchange 别名
    public DirectExchange  xExchange(){

        return  new DirectExchange(exchangeName);
    }

    @Bean("yExchange")
    public DirectExchange  yExchange(){

        return  new DirectExchange(exchangeName2);
    }

    //声明队列 ,过期时间ttl 10秒
    @Bean("queueA")
    public Queue queueA(){

        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-message-ttl",10000);
        // 2 正常队列设置死信交换机
        arguments.put("x-dead-letter-exchange", exchangeName2);
        //设置死信routingkey
        arguments.put("x-dead-letter-routing-key", "YD");
//        //正常队列的长度的限制
//        arguments.put("x-max-length",6);
        return QueueBuilder.durable(queuename1).withArguments(arguments).build();
    }

    //声明队列 ,过期时间ttl 30秒
    @Bean("queueB")
    public Queue queueB(){

        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-message-ttl",30000);
        // 2 正常队列设置死信交换机
        arguments.put("x-dead-letter-exchange", exchangeName2);
        //设置死信routingkey
        arguments.put("x-dead-letter-routing-key", "YD");
//        //正常队列的长度的限制
//        arguments.put("x-max-length",6);
        return QueueBuilder.durable(queuename2).withArguments(arguments).build();
    }

    //声明队列 ,过期时间ttl 30秒
    @Bean("queueC")
    public Queue queueC(){

        Map<String, Object> arguments = new HashMap<>();

        // 2 正常队列设置死信交换机
        arguments.put("x-dead-letter-exchange", exchangeName2);
        //设置死信routingkey
        arguments.put("x-dead-letter-routing-key", "YD");
//        //正常队列的长度的限制
//        arguments.put("x-max-length",6);
        return QueueBuilder.durable(queuename4).withArguments(arguments).build();
    }

    //死信队列
    @Bean("queueD")
    public Queue queueD(){

        return QueueBuilder.durable(queuename3).build();
    }

    //绑定
    @Bean
    public  Binding queueAbindingx(@Qualifier("queueA") Queue queueA,
                                   @Qualifier("xExchange")DirectExchange xexchange){
        return  BindingBuilder.bind(queueA).to(xexchange).with("XA");

    }

    //绑定
    @Bean
    public  Binding queueBbindingx(@Qualifier("queueB") Queue queueB,
                                   @Qualifier("xExchange")DirectExchange xexchange){
        return  BindingBuilder.bind(queueB).to(xexchange).with("XB");

    }
    //绑定
    @Bean
    public  Binding queueDbindingy(@Qualifier("queueD") Queue queueD,
                                   @Qualifier("yExchange")DirectExchange yexchange){
        return  BindingBuilder.bind(queueD).to(yexchange).with("YD");

    }

    //绑定
    @Bean
    public  Binding queueCbindingx(@Qualifier("queueC") Queue queueC,
                                   @Qualifier("xExchange")DirectExchange xexchange){
        return  BindingBuilder.bind(queueC).to(xexchange).with("XC");

    }


}
